home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Warrior’s Progress / source code / Source / Libraries / Drawing / Rectangle.cp < prev    next >
Encoding:
Text File  |  1997-06-28  |  7.4 KB  |  374 lines  |  [TEXT/CWIE]

  1. // Rectangle.cp
  2.  
  3. #ifndef Rectangle_h
  4. #include "Rectangle.h"
  5. #endif
  6. #ifndef MinMax_h
  7. #include "MinMax.h"
  8. #endif
  9.  
  10. const Rectangle Rectangle::big( minint16, minint16, maxint16, maxint16 );
  11. const Rectangle Rectangle::zero( 0, 0, 0, 0 );
  12.  
  13. Rectangle::Rectangle( Point p1, Point p2 )
  14.   {
  15.     if ( p1.h <= p2.h )
  16.       {
  17.         left = p1.h;
  18.         right = p2.h;
  19.       }
  20.      else
  21.       {
  22.         left = p2.h;
  23.         right = p1.h;
  24.       }
  25.     
  26.     if ( p1.v <= p2.v )
  27.       {
  28.         top = p1.v;
  29.         bottom = p2.v;
  30.       }
  31.      else
  32.       {
  33.         top = p2.v;
  34.         bottom = p1.v;
  35.       }
  36.   }
  37.  
  38. bool Rectangle::Contains( Point p ) const
  39.   {
  40.     return left <= p.h && p.h < right
  41.          && top <= p.v && p.v < bottom;
  42.   }
  43.  
  44. bool Rectangle::StrictlyContains( Point p ) const
  45.   {
  46.     return left < p.h && p.h < right
  47.          && top < p.v && p.v < bottom;
  48.   }
  49.  
  50. bool Rectangle::WeaklyContains( Point p ) const
  51.   {
  52.     return left <= p.h && p.h <= right
  53.          && top <= p.v && p.v <= bottom;
  54.   }
  55.  
  56. bool Rectangle::operator==( Rect r ) const
  57.   {
  58.     return left == r.left && right == r.right 
  59.          && top == r.top && bottom == r.bottom;
  60.   }
  61.  
  62. bool Rectangle::operator<=( Rect r ) const
  63.   {
  64.     return left >= r.left && right <= r.right 
  65.          && top >= r.top && bottom <= r.bottom;
  66.   }
  67.  
  68. bool Rectangle::operator>=( Rect r ) const
  69.   {
  70.     return left <= r.left && right >= r.right 
  71.          && top <= r.top && bottom >= r.bottom;
  72.   }
  73.  
  74. void Rectangle::operator&=( Rect r )
  75.   {
  76.     left = Max( left, r.left );
  77.     top = Max( top, r.top );
  78.     right = Min( right, r.right );
  79.     bottom = Min( bottom, r.bottom );
  80.     
  81.     if ( IsEmpty() )
  82.         *this = Rectangle( left, top, left, top );
  83.   }
  84.  
  85. void Rectangle::operator|=( Rect r )
  86.   {
  87.     left = Min( left, r.left );
  88.     top = Min( top, r.top );
  89.     right = Max( right, r.right );
  90.     bottom = Max( bottom, r.bottom );
  91.   }
  92.  
  93. Rectangle Rectangle::operator&( Rect r ) const
  94.   {
  95.     Rectangle result( *this );
  96.     result &= r;
  97.     return result;
  98.   }
  99.  
  100. Rectangle Rectangle::operator|( Rect r ) const
  101.   {
  102.     Rectangle result( *this );
  103.     result |= r;
  104.     return result;
  105.   }
  106.  
  107. bool Rectangle::Intersects( Rect r ) const
  108.   {
  109.     if ( left >= r.right || r.left >= right )
  110.         return false;
  111.     
  112.     if ( top >= r.bottom || r.top >= bottom )
  113.         return false;
  114.     
  115.     return true;
  116.   }
  117.  
  118. bool Rectangle::Touches( Rect r ) const
  119.   {
  120.     if ( left > r.right || r.left > right )
  121.         return false;
  122.     
  123.     if ( top > r.bottom || r.top > bottom )
  124.         return false;
  125.     
  126.     return true;
  127.   }
  128.  
  129. uint32 Rectangle::SharedSides( Rect r ) const
  130.   {
  131.     return ( ( left   == r.left   ) ? 1 : 0 )
  132.           + ( ( top    == r.top    ) ? 1 : 0 )
  133.           + ( ( right  == r.right  ) ? 1 : 0 )
  134.           + ( ( bottom == r.bottom ) ? 1 : 0 );
  135.   }
  136.  
  137. void Rectangle::operator+=( Rect r )
  138.   {
  139.     Assert( SharedSides( r ) == 3 );
  140.     Assert( !Intersects( r ) );
  141.     *this |= r;
  142.   }
  143.  
  144. void Rectangle::operator-=( Rect r )
  145.   {
  146.     Assert( SharedSides( r ) == 3 );
  147.     Assert( *this > r );
  148.     if ( left < r.left )
  149.         right = r.left;
  150.     if ( top < r.top )
  151.         bottom = r.top;
  152.     if ( right > r.right )
  153.         left = r.right;
  154.     if ( bottom > r.bottom )
  155.         top = r.bottom;
  156.   }
  157.  
  158. Rectangle Rectangle::operator+( Rect r ) const
  159.   {
  160.     Rectangle result( *this );
  161.     result += r;
  162.     return result;
  163.   }
  164.  
  165. Rectangle Rectangle::operator-( Rect r ) const
  166.   {
  167.     Rectangle result( *this );
  168.     result -= r;
  169.     return result;
  170.   }
  171.  
  172. void Rectangle::Inset( int16 amount )
  173.   {
  174.     Assert( CanAdd( top, amount ) );
  175.     Assert( CanAdd( left, amount ) );
  176.     Assert( CanSubtract( bottom, amount ) );
  177.     Assert( CanSubtract( right, amount ) );
  178.  
  179.     Assert( top + amount <= bottom - amount );
  180.     Assert( left + amount <= right - amount );
  181.  
  182.     top += amount;
  183.     left += amount;
  184.     bottom -= amount;
  185.     right -= amount;
  186.   }
  187.  
  188. void Rectangle::Inset( Point amount )
  189.   {
  190.     Assert( CanAdd( top, amount.v ) );
  191.     Assert( CanAdd( left, amount.h ) );
  192.     Assert( CanSubtract( bottom, amount.v ) );
  193.     Assert( CanSubtract( right, amount.h ) );
  194.  
  195.     Assert( top + amount.v <= bottom - amount.v );
  196.     Assert( left + amount.h <= right - amount.h );
  197.  
  198.     top += amount.v;
  199.     left += amount.h;
  200.     bottom -= amount.v;
  201.     right -= amount.h;
  202.   }
  203.  
  204. void Rectangle::Inset( Rect amount )
  205.   {
  206.     Assert( CanSubtract( top, amount.top ) );
  207.     Assert( CanSubtract( left, amount.left ) );
  208.     Assert( CanSubtract( bottom, amount.bottom ) );
  209.     Assert( CanSubtract( right, amount.right ) );
  210.  
  211.     Assert( top - amount.top <= bottom - amount.bottom );
  212.     Assert( left - amount.left <= right - amount.right );
  213.  
  214.     top -= amount.top;
  215.     left -= amount.left;
  216.     bottom -= amount.bottom;
  217.     right -= amount.right;
  218.   }
  219.  
  220. void Rectangle::Outset( int16 amount )
  221.   {
  222.     Assert( CanSubtract( top, amount ) );
  223.     Assert( CanSubtract( left, amount ) );
  224.     Assert( CanAdd( bottom, amount ) );
  225.     Assert( CanAdd( right, amount ) );
  226.  
  227.     Assert( top - amount <= bottom + amount );
  228.     Assert( left - amount <= right + amount );
  229.  
  230.     top -= amount;
  231.     left -= amount;
  232.     bottom += amount;
  233.     right += amount;
  234.   }
  235.  
  236. void Rectangle::Outset( Point amount )
  237.   {
  238.     Assert( CanSubtract( top, amount.v ) );
  239.     Assert( CanSubtract( left, amount.h ) );
  240.     Assert( CanAdd( bottom, amount.v ) );
  241.     Assert( CanAdd( right, amount.h ) );
  242.  
  243.     Assert( top - amount.v <= bottom + amount.v );
  244.     Assert( left - amount.h <= right + amount.h );
  245.  
  246.     top -= amount.v;
  247.     left -= amount.h;
  248.     bottom += amount.v;
  249.     right += amount.h;
  250.   }
  251.  
  252. void Rectangle::Outset( Rect amount )
  253.   {
  254.     Assert( CanAdd( top, amount.top ) );
  255.     Assert( CanAdd( left, amount.left ) );
  256.     Assert( CanAdd( bottom, amount.bottom ) );
  257.     Assert( CanAdd( right, amount.right ) );
  258.  
  259.     Assert( top + amount.top <= bottom + amount.bottom );
  260.     Assert( left + amount.left <= right + amount.right );
  261.  
  262.     top += amount.top;
  263.     left += amount.left;
  264.     bottom += amount.bottom;
  265.     right += amount.right;
  266.   }
  267.  
  268. void Rectangle::operator+=( Point vector )
  269.   {
  270.     Assert( CanAdd( top, vector.v ) );
  271.     Assert( CanAdd( left, vector.h ) );
  272.     Assert( CanAdd( bottom, vector.v ) );
  273.     Assert( CanAdd( right, vector.h ) );
  274.     top += vector.v;
  275.     left += vector.h;
  276.     bottom += vector.v;
  277.     right += vector.h;
  278.   }
  279.  
  280. void Rectangle::operator-=( Point vector )
  281.   {
  282.     Assert( CanSubtract( top, vector.v ) );
  283.     Assert( CanSubtract( left, vector.h ) );
  284.     Assert( CanSubtract( bottom, vector.v ) );
  285.     Assert( CanSubtract( right, vector.h ) );
  286.     top -= vector.v;
  287.     left -= vector.h;
  288.     bottom -= vector.v;
  289.     right -= vector.h;
  290.   }
  291.  
  292. Rectangle Rectangle::operator+( Point vector ) const
  293.   {
  294.     Rectangle result( *this );
  295.     result += vector;
  296.     return result;
  297.   }
  298.  
  299. Rectangle Rectangle::operator-( Point vector ) const
  300.   {
  301.     Rectangle result( *this );
  302.     result -= vector;
  303.     return result;
  304.   }
  305.  
  306. PointObject Rectangle::Size() const
  307.   {
  308.     Assert( Height() <= maxint16 );
  309.     Assert( Width() <= maxint16 );
  310.     return PointObject( Width(), Height() );
  311.   }
  312.  
  313. uint32 Rectangle::Bulk() const
  314.   {
  315.     return Height() + Width();
  316.   }
  317.  
  318. uint32 Rectangle::Area() const
  319.   {
  320.     return Height() * Width();
  321.   }
  322.  
  323. uint32 Rectangle::TaxicabDistanceTo( Point p ) const
  324.   {
  325.     uint32 distance = 0;
  326.     
  327.     if ( p.h < left )
  328.         distance += int32( left ) - int32( p.h );
  329.      else
  330.         if ( p.h > right )
  331.             distance += int32( p.h ) - int32( right );
  332.     
  333.     if ( p.v < top )
  334.         distance += int32( top ) - int32( p.v );
  335.      else
  336.         if ( p.v > bottom )
  337.             distance += int32( p.v ) - int32( bottom );
  338.     
  339.     return distance;
  340.   }
  341.  
  342. uint32 Rectangle::TaxicabDistanceTo( Rect r ) const
  343.   {
  344.     uint32 distance = 0;
  345.     
  346.     if ( r.right < left )
  347.         distance += int32( left ) - int32( r.right );
  348.      else
  349.         if ( r.left > right )
  350.             distance += int32( r.left ) - int32( right );
  351.     
  352.     if ( r.bottom < top )
  353.         distance += int32( top ) - int32( r.bottom );
  354.      else
  355.         if ( r.top > bottom )
  356.             distance += int32( r.top ) - int32( bottom );
  357.     
  358.     return distance;
  359.   }
  360.  
  361. void Rectangle::GlobalToLocal()
  362.   {
  363.     PointObject offset( 0, 0 );
  364.     offset.GlobalToLocal();
  365.     *this += offset;
  366.   }
  367.  
  368. void Rectangle::LocalToGlobal()
  369.   {
  370.     PointObject offset( 0, 0 );
  371.     offset.LocalToGlobal();
  372.     *this += offset;
  373.   }
  374.